let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    filter: [
      { id: 1, name: 'تىپى', type: 0 },
      { id: 2, name: 'كىتاب', type: 1 },
      { id: 3, name: 'ھەقسىز', type: 2 },
      { id: 4, name: 'قىزغىنلىق', type: 3 }
    ],
    filterKitab: [
      { id: 1, name: 'ھەممىسى' },
      { id: 2, name: 'ئاۋازلىق' },
      { id: 3, name: 'خەتلىك' }
    ],
    filterHaksiz: [
      { id: 1, name: 'ھەممىسى' },
      { id: 2, name: 'ھەقلىق' },
      { id: 3, name: 'ھەقسىز' }
    ],
    filterKizhinlik: [
      { id: 1, name: 'كۆپ ئاڭلىغان' },
      { id: 2, name: 'كۆپ ئوقۇلغان' },
      { id: 3, name: 'كۆپ ساقلانغان' },
      { id: 4, name: 'يېڭى يولانغان' },
      { id: 5, name: 'كۆپ ئىزدىگەن' },
      { id: 6, name: 'كۆپ ئىنكاس' }
    ],
    isLoading: true, // 是否加载完成
    isFilter: false,
    filterCon: [],
    filterConAc: 1,
    filterType: 0,
    bolum_id: -1,
    kitap_test: [],
    filterActive: 0,
    pageNo: 1,
    pageSize: 30,
    status: 1,
    recommend: '',
    is_free: '',
    video_source: '',
    isLanguageTypeValue: '',
    keyWord: '',
    categoryId: '',

    categoryId_active: 1,
    kitab_active: 1,
    is_free_active: 1,
    redu_active: 1,
  },

  // 获取所有书籍
  getPost() {
    this.setData({ isLoading: true })
    let that = this;
    wx.request({
      url: `${app.globalData.API_URL}/kitap`,
      data: {
        pageNo: this.data.pageNo,
        pageSize: this.data.pageSize,
        status: this.data.status,
        recommend: this.data.recommend,
        is_free: this.data.is_free,
        categoryId: this.data.categoryId,
        video_source: this.data.video_source,
        isLanguageTypeValue: this.data.isLanguageTypeValue,
        keyWord: this.data.keyWord,
        bolum_id: this.data.bolum_id,
      },
      header: {
        'content-type': 'application/json'
      },
      success (res) {
        if(res.statusCode == 200) {
          that.setData({ kitap_test: res.data.data, isFilter: false, })
          that.LoadingFinish(); // 完成加载
        }
      }
    })
  },

  // 点击书籍
  onRecommend(item) {
    let postId = item.currentTarget.dataset.item.id;
    wx.navigateTo({
      url: `/pages/audioList/audioList?postId=`+postId,
    })
  },

  // 获取类目
  getCategory() {
    let that = this;
    wx.request({
      url: `${app.globalData.API_URL}/category?status=1`,
      header: {
        'content-type': 'application/json',
      },
      success(res) {
        if (res.statusCode == 200) {
          that.setData({ filterCon: res.data });
        }
      },
    });
  },

  // 点击筛选
  onClickFilter(e) {
    let itemType = e.currentTarget.dataset.type;

    switch (itemType) {
      case 0:
        this.setData({ filterType: 0 })
        this.getCategory()
        break;
      case 1:
        this.setData({ filterType: 1 })
        break;
      case 2:
        this.setData({ filterType: 2 })
        break;
      case 3:
        this.setData({ filterType: 3 })
        break;
      default:
        break;
    }

    if(itemType == this.data.filterActive) {
      this.setData({ filterActive: itemType, isFilter: !this.data.isFilter });
    } else {
      this.setData({ filterActive: itemType, isFilter: true });
    }
    
  },

  // 点击筛选内容
  onfilterCon(e) {
    let itemId = e.currentTarget.dataset.item.id;
    
    if(this.data.filterActive !==0) {
      switch (this.data.filterActive) {
        case 1:
          this.setData({ kitab_active: itemId })
          break;
        case 2:
          this.setData({ is_free_active: itemId })
          break;
        case 3:
          this.setData({ redu_active: itemId })
         break;
        
        default:
          break;
      }
      
      return this.getPost()
    };
    this.setData({ filterConAc: itemId, categoryId: itemId })
    this.getPost();
  },

  // 取消筛选
  cancelFilter() {
    this.setData({ isFilter: false, filterActive: 0, filterConAc: 1 });
  },

  // 加载完成
  LoadingFinish() {
    setTimeout(() => {
      this.setData({ isLoading: false })
    }, 500)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.categor) {
      this.setData({ categoryId: options.categor, filterConAc: options.categor });
    }
    if(options.bolum_id) {
      this.setData({ bolum_id: options.bolum_id });
    }
    this.getPost();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: 'قىيات ئاۋازلىق ئەسەر',
      path: '/pages/books/books',
      imageUrl: 'https://mmecoa.qpic.cn/sz_mmecoa_png/iauAIibMdU78thJHXKBcIfYLNA6zH3pbFllMLlSibhVriaGwtS0bM1tLLyz6OSlQ74icF6c4Yic4icLokYGvb2jXnY8bA/640?wx_fmt=png&amp;from=appmsg'
    };
  }
})